/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "LUA_Component.h"
#include "../gui/GUI_Manager.h"
#include "../gui/Component.h"
//LUA_Component::LUA_Component(void)
//{
//    m_comp = NULL;
//}

LUA_Component::~LUA_Component(void)
{
    
}

/** Get the create a new component */
int LUA_Component::Create( lua_State *L )
{
    // retrieve the name from lua
    std::string name   = luaL_checkstring( L, 1 );
    // retrieve the potential owner
    std::string sowner = luaL_checkstring( L, 2 );
    const gui::Component* owner = gui::GUI_Manager::GetInstance()->GetParentComponent( sowner );
    
    m_comp = new gui::Component( owner ); // might be NULL
    m_comp->SetName( name );
    if ( !owner )
        gui::GUI_Manager::GetInstance()->AddComponent( m_comp );

    return 0;

}

int LUA_Component::Destroy( lua_State *L )
{
    if ( m_comp )
    {
        gui::GUI_Manager::GetInstance()->RemoveComponent( m_comp );
    }
    return 0;
}

/** Set/Get the name of this component */
int LUA_Component::SetName( lua_State *L )
{
    if ( m_comp )
    {
        // retrieve the name from lua
        std::string name   = luaL_checkstring( L, 1 );
        m_comp->SetName( name );
    }
    return 0;
}
int LUA_Component::GetName( lua_State *L )
{
    if ( m_comp )
    {
        lua_pushstring( L, m_comp->GetName().c_str() ); 
        return 1;
    }
    return 0;
}

/** Set/Get the location */
int LUA_Component::SetLocation( lua_State *L )
{
    if ( m_comp )
    {
        // retrieve the name from lua
        float x   = luaL_checknumber( L, 1 );
        float y   = luaL_checknumber( L, 2 );
        m_comp->SetLocation( Vector2f( x, y ) );
    }
    return 0;

}
int LUA_Component::GetLocation( lua_State *L )
{
    if ( m_comp )
    {
        Vector2f v;
        v.Set( m_comp->GetLocation() );
        lua_pushnumber( L, v.x );
        lua_pushnumber( L, v.y );
        return 2;
    }
    return 0;   
}

/** Set/Get the size */
int LUA_Component::SetSize( lua_State *L )
{
    if ( m_comp )
    {
        // retrieve the name from lua
        float x   = luaL_checknumber( L, 1 );
        float y   = luaL_checknumber( L, 2 );
        m_comp->SetSize( x, y );
    }
    return 0;
}
int LUA_Component::GetWidth( lua_State *L )
{
    if ( m_comp )
    {
        lua_pushnumber( L, m_comp->GetWidth() );
        return 1;
    }
    return 0;
}
int LUA_Component::GetHeight( lua_State *L )
{
    if ( m_comp )
    {
        lua_pushnumber( L, m_comp->GetHeight() );
        return 1;
    }
    return 0;
}

/** Set/Get Caption */
int LUA_Component::SetCaption( lua_State *L )
{
    if ( m_comp )
    {
        // retrieve the name from lua
        std::string cap  = luaL_checkstring( L, 1 );
        m_comp->SetName( cap );
    }
    return 0;
}
int LUA_Component::GetCaption( lua_State *L )
{
    if ( m_comp )
    {
        lua_pushstring( L, m_comp->GetCaption().c_str() );
        return 1;
    }
    return 0;
}

/** Set/Get background color */
int LUA_Component::SetBackgroundColor( lua_State *L )
{
    if ( m_comp )
    {
        // retrieve the name from lua
        float r   = luaL_checknumber( L, 1 );
        float g   = luaL_checknumber( L, 2 );
        float b   = luaL_checknumber( L, 3 );
        m_comp->SetBackgroundColor( Vector3f(r,g,b) );
    }
    return 0;
}
int LUA_Component::GetBackgroundColor( lua_State *L )
{
    if ( m_comp )
    {
        Vector3f v;
        v.Set( m_comp->GetBackgroundColor() );
        lua_pushnumber( L, v.GetX() );
        lua_pushnumber( L, v.GetY() );
        lua_pushnumber( L, v.GetZ() );
        return 3;
    }
    return 0;
}

/** Set/Get background color */
int LUA_Component::SetForegroundColor( lua_State *L )
{
    if ( m_comp )
    {
        // retrieve the name from lua
        float r   = luaL_checknumber( L, 1 );
        float g   = luaL_checknumber( L, 2 );
        float b   = luaL_checknumber( L, 3 );
        m_comp->SetForegroundColor( Vector3f(r,g,b) );

    }
    return 0;
}
int LUA_Component::GetForegroundColor( lua_State *L )
{
    if ( m_comp )
    {
        Vector3f v;
        v.Set( m_comp->GetForegroundColor() );
        lua_pushnumber( L, v.GetX() );
        lua_pushnumber( L, v.GetY() );
        lua_pushnumber( L, v.GetZ() );
        return 3;
    }
    return 0;
}

/** Set/Get Alpha */
int LUA_Component::SetAlpha( lua_State *L )
{
    if ( m_comp )
    {
        // retrieve the name from lua
        float a  = luaL_checknumber( L, 1 );
        m_comp->SetAlpha( a );
    }
    return 0;
}
int LUA_Component::GetAlpha( lua_State *L )
{
    if ( m_comp )
    {
        lua_pushnumber( L, m_comp->GetAlpha() );
        return 1;
    }
    return 0;
}

/** Set/Get focus */
int LUA_Component::SetFocus( lua_State *L )
{
    if ( m_comp )
    {
        int f  = (int)luaL_checknumber( L, 1 );
        m_comp->SetFocus( f != 0 );
    }
    return 0;
}
int LUA_Component::HasFocus( lua_State *L )
{
    if ( m_comp )
    {
        lua_pushboolean( L, m_comp->HasFocus() );
        return 1;
    }
    return 0;
}

/** Set/Get visibility */
int LUA_Component::SetVisible( lua_State *L )
{
    if ( m_comp )
    {
        int f  = (int)luaL_checknumber( L, 1 );
        m_comp->SetVisible( f != 0 );
    }
    return 0;
}

int LUA_Component::IsVisible( lua_State *L )
{
    if ( m_comp )
    {
        lua_pushboolean( L, m_comp->IsVisible() );
        return 1;
    }
    return 0;
}

// -- Bind to LUA
#define method(class, name) {#name, &class::##name}

const char LUA_Component::className[] = "GUI_Component";
Lunar<LUA_Component>::RegType LUA_Component::methods[] = {
        method(LUA_Component, Create),
        method(LUA_Component, Destroy),
        method(LUA_Component, SetName),
        method(LUA_Component, GetName),
        method(LUA_Component, SetCaption),
        method(LUA_Component, GetCaption),
        method(LUA_Component, SetLocation),
        method(LUA_Component, GetLocation),
        method(LUA_Component, SetSize),
        method(LUA_Component, GetWidth),
        method(LUA_Component, GetHeight),
        method(LUA_Component, SetBackgroundColor),
        method(LUA_Component, GetBackgroundColor),
        method(LUA_Component, SetForegroundColor),
        method(LUA_Component, GetForegroundColor),
        method(LUA_Component, SetAlpha),
        method(LUA_Component, GetAlpha),
        method(LUA_Component, SetFocus),
        method(LUA_Component, HasFocus),
        method(LUA_Component, SetVisible),
        method(LUA_Component, IsVisible),
        {NULL, NULL}
};

